package com.qyhl.webtv.module_live.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.live.TeleTextMessageBean;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoThemeBean;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.common.LiveUrl;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import razerdp.basepopup.BasePopupWindow;

public class ReportPopupWindow extends BasePopupWindow {
    private View popupView;

    private EditText reasonEdt;
    private TextView nicknameTv;
    private TextView contentTv;
    private TagFlowLayout tagFlowLayout;
    private TextView commitBtn;
    private ImageButton cancelBtn;
    private TeleTextMessageBean messageBean;

    private List<String> strList = new ArrayList<>();

    private String sceneId;


    public ReportPopupWindow(Activity context, String sceneId, TeleTextMessageBean bean) {
        super(context);
        messageBean = bean;
        this.sceneId = sceneId;
        bindEvent(context, messageBean);
    }

    @SuppressLint("SetTextI18n")
    private void bindEvent(Context context, TeleTextMessageBean bean) {
        if (popupView != null) {
            reasonEdt = popupView.findViewById(R.id.reason);
            nicknameTv = popupView.findViewById(R.id.nickName);
            contentTv = popupView.findViewById(R.id.content);
            tagFlowLayout = popupView.findViewById(R.id.tag_layout);
            commitBtn = popupView.findViewById(R.id.commit_btn);
            cancelBtn = popupView.findViewById(R.id.cancel_btn);
        }

        nicknameTv.setText(bean.getNickname() + "：");
        contentTv.setText(bean.getMessage());
        strList.clear();
        strList.add("垃圾广告营销");
        strList.add("侮辱谩骂");
        strList.add("违法反动");
        strList.add("淫秽色情");
        strList.add("嘲讽/不友善");
        strList.add("其他");

        tagFlowLayout.setAdapter(new TagAdapter<String>(strList) {
            @SuppressLint("SetTextI18n")
            @Override
            public View getView(FlowLayout parent, int position, String str) {
                TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.live_item_report_popup_tag,
                        tagFlowLayout, false);
                tv.setText(str);
                return tv;
            }
        });

        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (position == 5) {
                    reasonEdt.setVisibility(View.VISIBLE);
                } else {
                    reasonEdt.setVisibility(View.GONE);
                }
                return false;
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        commitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tagFlowLayout.getSelectedList().size() < 1) {
                    Toast.makeText(context, "请选择举报类型", Toast.LENGTH_SHORT).show();
                } else {
                    //获取标签
                    Set<Integer> tagsList = tagFlowLayout.getSelectedList();
                    int type = 0;
                    for (int position : tagsList) {
                        type = position;
                    }
                    String reason;
                    if (type == 5) {
                        if (TextUtils.isEmpty(reasonEdt.getText().toString())) {
                            Toast.makeText(context, "请填写举报原因！", Toast.LENGTH_SHORT).show();
                            return;
                        } else {
                            reason = reasonEdt.getText().toString();
                        }
                    } else {
                        reason = strList.get(type);
                    }

                    EasyHttp.post(LiveUrl.GET_TELETEXT_REPORT)
                            .params("sceneId", sceneId)
                            .params("userName", bean.getUsername())
                            .params("nickName", bean.getNickname())
                            .params("chatText", bean.getMessage())
                            .params("reporterName", CommonUtils.getInstance().getUserName())
                            .params("reporterNickname", CommonUtils.getInstance().getNickName())
                            .params("reporterText", reason)
                            .execute(new SimpleCallBack<String>() {
                                @Override
                                public void onError(ApiException e) {
                                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onSuccess(String bean) {
                                    Toast.makeText(context, "举报成功！", Toast.LENGTH_SHORT).show();
                                    dismiss();
                                }
                            });
                }

            }
        });
    }

    @Override
    protected Animation initShowAnimation() {
        return getDefaultScaleAnimation();
    }


    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.live_report_popup_window, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }
}
